﻿using System;
using System.Collections.Generic;
using System.Diagnostics;

using STMS.DAL;
using STMS.Models.DModels;

namespace STMS.BLL
{
    public class StoreBLL
    {
        private StoreDAL storeDAL = new StoreDAL();

        public List<StoreInfo> GetStoreList(string keywords, bool isDel)
        {
            int isDeleted = isDel ? 1 : 0;
            return storeDAL.GetStoreList(keywords, isDeleted);
        }

        /// <summary>
        /// 判断当前仓库是否已经添加过区域
        /// </summary>
        /// <param name="storeId"></param>
        /// <returns></returns>
        public bool IsAddRegion(int storeId)
        {
            int count = storeDAL.GetStoreRegionCount(storeId);
            if (count > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 假删除
        /// </summary>
        /// <param name="storeId"></param>
        /// <returns></returns>
        public int LogicDeleteStore(int storeId)
        {
            int reDel = 0;//失败
            if (IsAddRegion(storeId))
                return reDel = 2;// 已经添加了分区，不能删除
            else
            {
                bool blDel = storeDAL.Delete(storeId, 0, 1);
                if (blDel)
                    reDel = 1;//删除成功
                else
                    reDel = 0;//失败
            }
            return reDel;
        }
        /// <summary>
        /// 获取所有仓库
        /// </summary>
        /// <returns></returns>
        public List<StoreInfo> GetAllStores()
        {
            return storeDAL.GetAllStores();
        }
        /// <summary>
        /// 批量假删除
        /// </summary>
        /// <param name="storeIds"></param>
        /// <returns></returns>
        public string LogicDeleteStore(List<int> storeIds)
        {
            string reStr = "";
            foreach (int storeId in storeIds)
            {
                if (reStr.Length > 0)
                    reStr += ",";
                // 检查 IsDeleted 属性
                if (IsStoreDeleted(storeId))
                    reStr += storeId + " (已删除)";
                //仓库有分区
                else if (IsAddRegion(storeId))
                    reStr += storeId;
            }
            //没有添加分区的仓库
            if (reStr == "")
            {
                reStr = storeDAL.DeleteList(storeIds, 0, 1) ? "Y" : "0";
            }

            return reStr;
        }
        /// <summary>
        /// 检查仓库是否已被删除
        /// </summary>
        /// <param name="storeId"></param>
        /// <returns></returns>
        private bool IsStoreDeleted(int storeId)
        {
            //int result = storeDAL.GetStoreById(storeId);
            StoreInfo storeInfo = storeDAL.GetStore(storeId);
            return storeInfo != null && storeInfo.IsDeleted == 1;
        }

        /// <summary>
        /// 恢复
        /// </summary>
        /// <param name="storeId"></param>
        /// <returns></returns>
        public bool ReceiveStore(int storeId)
        {
            return storeDAL.Delete(storeId, 0, 0);
        }

        /// <summary>
        /// 真删除（移除）
        /// </summary>
        /// <param name="storeId"></param>
        /// <returns></returns>
        public bool DeleteStore(int storeId)
        {
            return storeDAL.Delete(storeId, 1, 2);
        }

        /// <summary>
        /// 获取指定的仓库信息
        /// </summary>
        /// <param name="storeId"></param>
        /// <returns></returns>
        public StoreInfo GetStore(int storeId)
        {
            return storeDAL.GetStore(storeId);
        }

        /// <summary>
        /// 检查编码或名称是否已经存在
        /// </summary>
        /// <param name="storeName"></param>
        /// <param name="storeNo"></param>
        /// <returns></returns>
        public int ExistsStore(string storeName, string storeNo)
        {
            bool[] bls = storeDAL.ExistsStore(storeName, storeNo);
            bool blName = bls[0];
            bool blNo = bls[1];

            if (blName && blNo)
                return 3;//都存在
            else if (blName)
                return 1;//Name 存在
            else if (blNo)
                return 2;//No 存在
            else
                return 0;//都不存在
        }

        public List<StoreInfo> getAllStore()
        {
            return storeDAL.GetStoreList();
        }

        /// <summary>
        /// 添加仓库
        /// </summary>
        /// <param name="storeInfo"></param>
        /// <returns></returns>
        public bool AddStore(StoreInfo storeInfo)
        {
            return storeDAL.AddStore(storeInfo, 0) > 0;
        }

        /// <summary>
        /// 添加仓库  返回id
        /// </summary>
        /// <param name="storeInfo"></param>
        /// <param name="isGetId"> 0 不返回 id   1 返回id</param>
        /// <returns></returns>
        public int AddStore(StoreInfo storeInfo, int isGetId)
        {
            return storeDAL.AddStore(storeInfo, isGetId);
        }

        /// <summary>
        /// 修改仓库信息
        /// </summary>
        /// <param name="storeInfo"></param>
        /// <returns></returns>
        public bool UpdateStore(StoreInfo storeInfo)
        {
            return storeDAL.UpdateStore(storeInfo);
        }

        
    }
}
